package cn.stylefeng.guns.modular.business.frequency.announcement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.CoreExceptionEnum;
import cn.stylefeng.guns.core.exception.CustomException;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.custom.constant.PublicCommon;
import cn.stylefeng.guns.modular.business.frequency.announcement.entity.Announcement;
import cn.stylefeng.guns.modular.business.frequency.announcement.enums.AnnouncementExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.announcement.mapper.AnnouncementMapper;
import cn.stylefeng.guns.modular.business.frequency.announcement.model.param.AnnouncementParam;
import cn.stylefeng.guns.modular.business.frequency.announcement.service.AnnouncementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公告 服务实现类
 *
 * @author 余杰
 * @date 2022/11/14 13:38
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    @Override
    public PageResult<Announcement> page(AnnouncementParam announcementParam) {

        // 构造条件
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Announcement::getDelFlag, PublicCommon.启用);
        if(LoginContextHolder.me().isSuperAdmin()){
            queryWrapper.eq(Announcement::getTenantId,1);
        }else {
            queryWrapper.and(wrapper->wrapper.eq(Announcement::getTenantId,1).or()
                    .eq(Announcement::getTenantId,LoginContextHolder.me().getSysLoginUser().getTenantId()));
        }

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Announcement> list(AnnouncementParam announcementParam) {

        // 构造条件
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(AnnouncementParam announcementParam) {

        // 将dto转为实体
        Announcement announcement = new Announcement();
        BeanUtil.copyProperties(announcementParam, announcement);
        announcement.setTenantId(LoginContextHolder.me().getSysLoginUser().getTenantId());

        this.save(announcement);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(AnnouncementParam announcementParam) {
        this.removeById(announcementParam.getId());
    }

    @Override
    public void edit(AnnouncementParam announcementParam) {

        // 根据id查询实体
        Announcement announcement = this.queryAnnouncementEdit(announcementParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(announcementParam, announcement);

        this.updateById(announcement);
    }

    @Override
    public Announcement detail(AnnouncementParam announcementParam) {
        return this.queryAnnouncement(announcementParam);
    }



    /**
     * 获取公告
     *
     * @author 余杰
     * @since 2022/11/14 13:38
     */
    private Announcement queryAnnouncement(AnnouncementParam announcementParam) {
        Announcement announcement = this.getById(announcementParam.getId());
        if (ObjectUtil.isEmpty(announcement)) {
            throw new ServiceException(AnnouncementExceptionEnum.NOT_EXISTED);
        }

        return announcement;
    }
    private Announcement queryAnnouncementEdit(AnnouncementParam announcementParam) {
        Announcement announcement = this.getById(announcementParam.getId());
        if (ObjectUtil.isEmpty(announcement)) {
            throw new ServiceException(AnnouncementExceptionEnum.NOT_EXISTED);
        }
        Long tenantId = LoginContextHolder.me().getSysLoginUser().getTenantId();
        //如果不是一个租户,禁止编辑
        if (!ObjectUtil.equal(tenantId,announcement.getTenantId())){
            throw new CustomException(CoreExceptionEnum.ERROR_PERMISSION);
        }
        return announcement;
    }
}